#!/usr/bin/env python
# encoding=utf-8

"""
脚本 `offcie_crawler.py` 需要定期执行（目前在我的电脑上按照每小时一次执行，目前在我的电脑上已有991279数据，位于 `mongo -> crawler_office`）。
每天执行一次 `calc_building_price.py`，用于计算办公楼的平均房价。
"""

import datetime
import sys

from pymongo import MongoClient
from scpy.logger import get_logger
from xtls.timeparser import now

from config import *

reload(sys)
sys.setdefaultencoding('utf-8')

__author__ = 'xlzd'
logger = get_logger(__file__)
MONGO = MongoClient(MONGO_HOST, MONGO_PORT)
MG_BUILDING_CONN = MONGO[DB_NAME]['officeBuilding']
MG_ITEMS_CONN = MONGO[DB_NAME]['officeItems']


def main():
    today = datetime.datetime.now().day

    for index, item in enumerate(MG_BUILDING_CONN.find().batch_size(100), start=1):

        datas = MG_ITEMS_CONN.find({
            'building': item['building'],
            'officeDistrict': item['officeDistrict']
        })
        count = datas.count()
        if count == 0:
            continue
        total = 0
        for office_item in datas:
            total += office_item['unitPrice']
        item['averagePrice'] = total / float(count)
        item['sampleCount'] = count
        item['updateTime'] = now()

        MG_BUILDING_CONN.update_one(
            filter={'_id': item['_id']},
            update={'$set': item},
            upsert=False
        )
        logger.info('updated building %s-%s-%s' % (index, item['officeDistrict'], item['building']))


if __name__ == '__main__':
    main()
